package gframework.bean.textArea;

import gframework.bean.field.GPlainDocument;
import java.awt.Color;
import java.awt.KeyboardFocusManager;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import javax.swing.KeyStroke;

public class GTextArea extends javax.swing.JTextArea {

    private boolean caixaAlta = true;

    public GTextArea() {
        initComponents();

        setDocument(new GPlainDocument());

        this.setWrapStyleWord(true);

        //configura tab
        HashSet kTab = new HashSet(1);
        kTab.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));

        setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, kTab);

        HashSet kShiftTab = new HashSet(1);
        kShiftTab.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));

        setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, kShiftTab);
    }

    @Override
    public void setCaretPosition(int position) {
        super.setCaretPosition(position);
    }

    public void setCaixaAlta(boolean i_caixaAlta) {
        caixaAlta = i_caixaAlta;
        ((GPlainDocument) getDocument()).setCaixaAlta(i_caixaAlta);
    }

    public boolean isCaixaAlta() {
        return caixaAlta;
    }

    public void clear() {
        this.setText("");
    }

    public void setTextBackground(Color c) throws Exception {
        super.setBackground(c);
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLineWrap(true);
        setWrapStyleWord(true);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        if (getColumns() <= this.getText().length()) {
            if (evt.getKeyCode() == KeyEvent.VK_BACK_SPACE || evt.getKeyCode() == KeyEvent.VK_DELETE) {
                this.setEditable(true);
            } else {
                this.setEditable(false);
                return;
            }
        }
    }//GEN-LAST:event_formKeyPressed

    private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
    }//GEN-LAST:event_formKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}